package hz.mobileproject.pda.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.entity.response.Rp_ContentSummary;
import hz.mobileproject.pda.ui.base.BaseCustomAdapter;
import hz.mobileproject.pda.ui.view.MyGridView;

/**
 * Created by Lc on 2017/12/25.
 */

public class ContentSummaryAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<Rp_ContentSummary> mList;
    private LayoutInflater mInflater;

    public ContentSummaryAdapter(Context context, List<Rp_ContentSummary> mList) {
        this.mContext = context;
        this.mList = mList;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return this.mList == null ? 0 : this.mList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
//        return this.mList.get(groupPosition).getChrilsList()==null?0:this.mList.get(groupPosition).getChrilsList().size();
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this.mList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return this.mList.get(groupPosition).getSub_data().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder vh;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_content_summary_group, null);
            vh = new GroupViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (GroupViewHolder) convertView.getTag();
        }
        vh.tv_title.setText(this.mList.get(groupPosition).getName());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChrildViewHolder vh;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_content_summary_chrild, null);
            vh = new ChrildViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ChrildViewHolder) convertView.getTag();
        }
        //TODO 动态设置parent name
        if (StringUtils.isEmpty(this.mList.get(groupPosition).getSub_data().get(childPosition).getParent_name())) {
            //为空才设置   减少设置次数
            this.mList.get(groupPosition).getSub_data().get(childPosition).setParent_name(this.mList.get(groupPosition).getName());
        }
        vh.gridView.setAdapter(new ChrildGridViewAdapter(mContext, this.mList.get(groupPosition).getSub_data()));

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


    class GroupViewHolder {
        @ViewInject(R.id.tv_title)
        private TextView tv_title;

        public GroupViewHolder(View view) {
            x.view().inject(this, view);
        }
    }

    class ChrildViewHolder {
        @ViewInject(R.id.gv_content_summary_chrild)
        private MyGridView gridView;

        public ChrildViewHolder(View view) {
            x.view().inject(this, view);
        }
    }

    public class ChrildGridViewAdapter extends BaseCustomAdapter<Rp_ContentSummary,ChrildGridViewAdapter.GvViewHolder> {

        public ChrildGridViewAdapter(Context mContext, List<Rp_ContentSummary> list) {
            super(mContext, list);
        }

        @Override
        protected int bindView() {
            return R.layout.item_edu_evaluate_checkbox;
        }

        @Override
        protected GvViewHolder createViewHolder() {
            return new GvViewHolder();
        }

        @Override
        protected void bindData(final int position, View convertView, GvViewHolder viewHolder) {
            viewHolder.checkBox.setChecked(this.mList.get(position).isChecked());
            viewHolder.checkBox.setText(this.mList.get(position).getName());
            viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    ChrildGridViewAdapter.this.mList.get(position).setChecked(isChecked);
//                    if (isChecked) {
//                        //如果点击是其他   弹窗输入
//                        if ("其他".equals(ChrildGridViewAdapter.this.mList.get(position).getParent_name())) {
//                            ContentSummaryOtherDialog dialog = new ContentSummaryOtherDialog(mContext);
//                            dialog.setTitle("其他内容添加").setMessage("输入其他内容描述")
//                                    .setContent(ChrildGridViewAdapter.this.mList.get(position).getName())
//                                    .setContentMinLength(5)
//                                    .setOnClickListener(new ContentSummaryOtherDialog.OnClickListener() {
//                                        @Override
//                                        public void onPositiveClick(String s) {
//                                            ChrildGridViewAdapter.this.mList.get(position).setChecked(true);
//                                            ChrildGridViewAdapter.this.mList.get(position).setName(s);
//                                            //TODO 调用外层adapter的刷新   里层gridview刷新会出现无用情况
//                                            ContentSummaryAdapter.this.notifyDataSetChanged();
//                                        }
//                                        @Override
//                                        public void onNagetiveClick() {
//                                            ChrildGridViewAdapter.this.mList.get(position).setChecked(false);
//                                            //TODO 调用外层adapter的刷新   里层gridview刷新会出现无用情况
//                                            ContentSummaryAdapter.this.notifyDataSetChanged();
//                                        }
//                                    }).show();
//                            //只有“其他”时   不显示
//                            if("其他".equals(ChrildGridViewAdapter.this.mList.get(position).getName())){
//                                dialog.setContent("");
//                            }
//                        }
//                    }
                }
            });
        }
        class GvViewHolder {
            @ViewInject(R.id.checkBox)
            private CheckBox checkBox;
        }
    }

    public List<Rp_ContentSummary> getmList() {
        return mList;
    }

    public void setmList(List<Rp_ContentSummary> mList) {
        if (mList == null) {
            mList = new ArrayList<>();
        }
        this.mList.clear();
        this.mList.addAll(mList);
        this.notifyDataSetChanged();
    }

    /**
     * 获取选中的列表
     *
     * @return
     */
    public List<Rp_ContentSummary> getCheckedList() {
        List<Rp_ContentSummary> list = new ArrayList<>();
        for (Rp_ContentSummary contentSummary : this.mList) {
            for (Rp_ContentSummary contentSummarySon : contentSummary.getSub_data()) {
                if (contentSummarySon.isChecked()) {
                    contentSummarySon.setParent_name(contentSummary.getName());
                    list.add(contentSummarySon);
                }
            }
        }
        return list;
    }
}
